<?php

/**
 * This is the model class for table "{{dispatchdetails}}".
 *
 * The followings are the available columns in table '{{dispatchdetails}}':
 * @property string $iddispatchdetails
 * @property string $iddispatch
 * @property string $idstockitems
 * @property string $dispatchquantity
 * @property string $dispatchprocessingrate
 * @property string $dispatchvalue
 * @property string $itemquantitytested
 * @property string $itemquantityloss
 * @property string $testreport
 * @property string $remark
 * @property string $testdate
 * @property string $sixsigmano
 *
 * The followings are the available model relations:
 * @property Dispatch $iddispatch0
 * @property Stockitems $idstockitems0
 * @property Ssctest[] $ssctests
 */
class Dispatchdetails extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @return Dispatchdetails the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{dispatchdetails}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('iddispatch, idstockitems, dispatchquantity, dispatchprocessingrate, dispatchvalue, itemquantitytested, itemquantityloss, testreport, remark, testdate, sixsigmano', 'required'),
			array('iddispatch, idstockitems, dispatchquantity, dispatchprocessingrate, dispatchvalue, itemquantitytested, itemquantityloss', 'length', 'max'=>10),
			array('testreport', 'length', 'max'=>50),
			array('remark', 'length', 'max'=>100),
			array('sixsigmano', 'length', 'max'=>20),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('iddispatchdetails, iddispatch, idstockitems, dispatchquantity, dispatchprocessingrate, dispatchvalue, itemquantitytested, itemquantityloss, testreport, remark, testdate, sixsigmano', 'safe', 'on'=>'search'),
                        array('invoiceno', 'autocompleteValidator', 'model'=>'Dispatch'),
                        array('stockitemsname', 'autocompleteValidator', 'model'=>'Stockitems'),
		);
	}        
        
        public function autocompleteValidator($attribute,$params){
               $match = addcslashes($_REQUEST[$attribute], '%_');
               if(!new $params['model']->exists(new CDbCriteria( array(
                    'condition' => "$attribute = :$attribute",
                    'params'    => array(":$attribute" => "$match")
                )))){
                    $this->addError($attribute, Yii::t('application', 'Incorrect {attribute}.', array('{attribute}'=>Yii::t('application', $attribute))));
               }
        }      

        public function beforeSave(){
                        if(isset($_REQUEST['invoiceno'])){
                $match = addcslashes($_REQUEST['invoiceno'], '%_'); // escape LIKE's special characters
                $criteria=new CDbCriteria( array(
                    'condition' => 'invoiceno = :invoiceno',
                    'params'    => array(':invoiceno' => "$match")
                ) );            
                $model=Dispatch::model()->find($criteria);
                $this->iddispatch = $model->iddispatch;
            }
                        if(isset($_REQUEST['stockitemsname'])){
                $match = addcslashes($_REQUEST['stockitemsname'], '%_'); // escape LIKE's special characters
                $criteria=new CDbCriteria( array(
                    'condition' => 'stockitemsname = :stockitemsname',
                    'params'    => array(':stockitemsname' => "$match")
                ) );            
                $model=Stockitems::model()->find($criteria);
                $this->idstockitems = $model->idstockitems;
            }
                        return parent::beforeSave();
        }        
        
       
        
        
	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'iddispatch0' => array(self::BELONGS_TO, 'Dispatch', 'iddispatch'),
			'idstockitems0' => array(self::BELONGS_TO, 'Stockitems', 'idstockitems'),
			'ssctests' => array(self::HAS_MANY, 'Ssctest', 'iddispatchdetails'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'iddispatchdetails' => Yii::t('application', 'Iddispatchdetails'),
			'iddispatch' => Yii::t('application', 'Iddispatch'),
			'idstockitems' => Yii::t('application', 'Idstockitems'),
			'dispatchquantity' => Yii::t('application', 'Dispatchquantity'),
			'dispatchprocessingrate' => Yii::t('application', 'Dispatchprocessingrate'),
			'dispatchvalue' => Yii::t('application', 'Dispatchvalue'),
			'itemquantitytested' => Yii::t('application', 'Itemquantitytested'),
			'itemquantityloss' => Yii::t('application', 'Itemquantityloss'),
			'testreport' => Yii::t('application', 'Testreport'),
			'remark' => Yii::t('application', 'Remark'),
			'testdate' => Yii::t('application', 'Testdate'),
			'sixsigmano' => Yii::t('application', 'Sixsigmano'),
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('iddispatchdetails',$this->iddispatchdetails,true);
		$criteria->compare('iddispatch',$this->iddispatch,true);
		$criteria->compare('idstockitems',$this->idstockitems,true);
		$criteria->compare('dispatchquantity',$this->dispatchquantity,true);
		$criteria->compare('dispatchprocessingrate',$this->dispatchprocessingrate,true);
		$criteria->compare('dispatchvalue',$this->dispatchvalue,true);
		$criteria->compare('itemquantitytested',$this->itemquantitytested,true);
		$criteria->compare('itemquantityloss',$this->itemquantityloss,true);
		$criteria->compare('testreport',$this->testreport,true);
		$criteria->compare('remark',$this->remark,true);
		$criteria->compare('testdate',$this->testdate,true);
		$criteria->compare('sixsigmano',$this->sixsigmano,true);

		return new CActiveDataProvider(get_class($this), array(
			'criteria'=>$criteria,
		));
	}
}